from fontTools.ttLib import TTFont
from PIL import Image, ImageDraw, ImageFont
import io
import ddddocr


ocr = ddddocr.DdddOcr(show_ad=False)
font_path = "./tu/ztjm/96fc7b50b772f52.woff2"
font = TTFont(font_path)
img_font = ImageFont.truetype(font_path,size=20)
font_maps ={

}
for code,name in font.getBestCmap().items():
    # print(code,chr(code),name,)
    img = Image.new('RGB',(25,25),(255,255,255))
    draw = ImageDraw.Draw(img)
    draw.text((5, -5), chr(code), (0,0,0), font=img_font)
    #
    # img.save(f"./tu/ztjm/{code}.png".format(code))

    # 在内存中创建一个字节流缓冲区，它模拟文件操作，但数据完全存储在内存中，不落地磁盘。
    img_bytes = io.BytesIO()
    img.save(img_bytes, format='PNG')

    ocr = ddddocr.DdddOcr(show_ad=False)
    r = ocr.classification(img_bytes.getvalue())
    print(code, "识别结果", r)
    font_maps[code] = r


def parse_field(value):
    result = ""
    for c in value:
        if ord(c) in font_maps:
            result += font_maps[ord(c)]
        else:
            result += c
    return result

import requests

res = requests.post('https://www.dongchedi.com/motor/pc/sh/sh_sku_list?aid=1839&app_name=auto_web_pc', data={
"sh_city_name":"全国",
"page":2,
"limit":20
})
for item in res.json()['data']['search_sh_sku_info_list']:
    print(item['car_name'], parse_field(item['car_source_city_name']), parse_field(item['official_price']), parse_field(item['sh_price']))